package com.sina.shop.dao.goods;

import com.sina.shop.dao.ShopDataSourceDao;
import com.sina.shop.domain.goods.Goods;
import com.sina.shop.domain.goods.Goods_;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-05-15:04
 */
@Repository("goodsDao")
public class GoodsDaoImpl extends ShopDataSourceDao implements GoodsDao {

    @Override
    public List<Goods> getGoodsList(Goods goods) {
        return template.executeWithNativeSession(session -> {

            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<Goods> criteriaQuery = builder.createQuery(Goods.class);
            Root<Goods> root = criteriaQuery.from(Goods.class);
            Predicate predicate = goodsPredicate(goods, builder, root);
            criteriaQuery.where(predicate);
            criteriaQuery.orderBy(
                    builder.desc(root.get(Goods_.id))
            );

            Query<Goods> query = session.createQuery(criteriaQuery);
            int first = (goods.getPage() - 1) * goods.getRows();
            query.setFirstResult(first);
            query.setMaxResults(goods.getRows());

            return query.getResultList();
        });
    }

    @Override
    public Long getGoodsCount(Goods goods) {
        return template.executeWithNativeSession(session -> {

            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
            Root<Goods> root = criteriaQuery.from(Goods.class);
            //根据id查找结果数
            criteriaQuery.select(
                    builder.count(root.get(Goods_.id))
            );
            //组装查询条件
            criteriaQuery.where(
                    goodsPredicate(goods, builder, root)
            );
            //创建插叙语句
            Query<Long> query = session.createQuery(criteriaQuery);
            long count;
            try {
                //得到结果条数
                count = query.getSingleResult();
            } catch (NoResultException e) {
                count = 0;
            }
            return count;
        });
    }

    protected Predicate goodsPredicate(Goods goods, CriteriaBuilder builder
            , Root<Goods> root) {
        List<Predicate> list = new ArrayList<>(4);
        if (goods.getLastId() != null) {
            list.add(
                    builder.lt(root.get(Goods_.id), goods.getLastId())
            );
        }
        //商品名字
        if (goods.getName() != null) {
            list.add(
                    builder.like(root.get(Goods_.name), "%" + goods.getName() + "%")
            );
        }
        //菜单
        if (goods.getMenu() != null) {
            list.add(
                    builder.equal(root.get(Goods_.menu), goods.getMenu().getId())
            );
        }
        //是否推荐
        if (goods.getIsGroom() != null) {
            list.add(
                    builder.equal(root.get(Goods_.isGroom), goods.getIsGroom())
            );
        }
        //信誉度
        if (goods.getCredit() != null) {
            list.add(
                    builder.equal(root.get(Goods_.credit), goods.getCredit())
            );
        }
        return builder.and(
                list.toArray(new Predicate[list.size()])
        );
    }

    @Override
    public Integer deleteGoods(Long id) {
        String hql = "delete from Goods g where g.id = :id";
        return template.executeWithNativeSession(session -> {
            Query query = session.createQuery(hql);
            query.setParameter("id", id);

            return query.executeUpdate();
        });
    }
}
